/**
 * 图形化设计的上下文信息
 *
 * @param data
 * @returns
 */
function Context(data) {
    this.id = data.id;
    this.readOnly = data.readOnly;
    this.x = 0;
    this.y = 0;
    this.container = $("#" + this.id);
    this.width = this.container.width();
    this.height = this.container.height();
    this.command = null;
    this.commandObject;
    this.paper = Raphael(data.id, this.width, this.height);
    this.paper.setSize(this.width, this.height);
    this.bakground = new Bakground(this);
    this.processDefinition = new ProcessDefinition();
    this.sequenceId = 0;
}

/**
 * 获取流程中任何信息的唯一关键字
 *
 * @returns {Number}
 */
Context.prototype.getUuid = function() {
    this.sequenceId = this.sequenceId - 1;
    return this.sequenceId;
};
/**
 * 是否存在已经定义的路由
 *
 * @param transition
 * @returns {Boolean}
 */
Context.prototype.isExistTransition = function(transition) {
    for (var i = 0, cnt = this.processDefinition.transitionList.length; i < cnt; i++) {
        if ((this.processDefinition.transitionList[i].startActivity.sid == transition.startActivity.sid) &&
        (this.processDefinition.transitionList[i].endActivity.sid == transition.endActivity.sid)) {
            return true;
        }
    }
    return false;
};

/**
 * 获取活动对应的路由
 *
 * @param transition
 * @returns {Object}
 */
Context.prototype.getTransitionOfActivity = function(activity) {
    var transitions = new Object();
    transitions.startActivityList = new Array();
    transitions.endActivityList = new Array();
    
    for (var i = 0, cnt = this.processDefinition.transitionList.length; i < cnt; i++) {
        if (this.processDefinition.transitionList[i].startActivity.sid == activity.sid) {
            transitions.startActivityList.add(this.processDefinition.transitionList[i]);
        }
        if (this.processDefinition.transitionList[i].endActivity.sid == activity.sid) {
            transitions.endActivityList.add(this.processDefinition.transitionList[i]);
        }
    }
    return transitions;
};

/**
 * 是否存在已经定义活动
 *
 * @param transition
 * @returns {Boolean}
 */
Context.prototype.isExistActivity = function(activity) {
    for (var i = 0, cnt = this.processDefinition.activityList.length; i < cnt; i++) {
        if (this.processDefinition.activityList[i].sid == activity.sid) {
            return true;
        }
    }
    return false;
};

Context.prototype.getActivity = function(activity) {
    for (var i = 0, cnt = this.processDefinition.activityList.length; i < cnt; i++) {
        if (this.processDefinition.activityList[i].sid == activity.sid) {
            return this.processDefinition.activityList[i];
        }
    }
    return null;
};

/**
 * 根据活动类型获取活动列表
 *
 * @param type
 * @returns activityList
 */
Context.prototype.getActivityByType = function(type) {
    var activityList = [];
    for (var i = 0, cnt = this.processDefinition.activityList.length; i < cnt; i++) {
        if (this.processDefinition.activityList[i].clazz == type) {
            activityList.push(this.processDefinition.activityList[i]);
        }
    }
    return activityList;
}

/**
 * 添加路由信息
 *
 * @param transition
 */
Context.prototype.addTransition = function(transition) {
    if (!this.isExistTransition(transition)) {
        this.processDefinition.transitionList.add(transition);
    } else {
        alert("路由已经存在，不能重复添加");
    }
};
/**
 * 删除路由信息
 *
 * @param transition
 */
Context.prototype.removeTransition = function(transition) {
    if (this.isExistTransition(transition)) {
        transition.remove();
        this.processDefinition.transitionList.remove(transition);
    }
};
/**
 * 添加活动
 *
 * @param activity
 */
Context.prototype.addActivity = function(activity) {
    if (!this.isExistActivity(activity)) {
        this.processDefinition.activityList.add(activity);
        return true;
    } else {
        alert("活动已经存在，不能重复添加");
        return false;
    }
};
/**
 * 删除活动
 *
 * @param activity
 */
Context.prototype.removeActivity = function(activity) {
    if (this.isExistActivity(activity)) {
        //activity.remove();
        this.processDefinition.activityList.remove(activity);
    }
};
Context.prototype.update = function(x, y) {
    this.x = x;
    this.y = y;
    if (this.width < x) {
        this.width = x;
    }
    if (this.height < y) {
        this.height = y;
    }
    this.paper.setSize(this.width, this.height);
    if (this.command == "line" && this.commandObject instanceof Transition && jQuery.isFunction(this.commandObject.updateEndXY)) {
        if (((x - 2) > 0) && (y - 2 > 0)) {
            var activityHotPoint = this.commandObject.startActivity.hotPoint;
            var sx = activityHotPoint.topMiddle.attrs.x;
            var sy = activityHotPoint.middleLeft.attrs.y;
            var result = compareCoordinate({
                x: sx,
                y: sy
            }, {
                x: x,
                y: y
            }, this.commandObject);
            if (this.commandObject.view.attrs.path.length == 2) {
                this.commandObject.updateStartXY(result.start.x, result.start.y);
            }
            this.commandObject.updateEndXY(result.end.x, result.end.y);
        }
    }
    //$("#canvas_status").html("x:" + x + " y:" + y);
};
Context.prototype.gridToggle = function() {
    this.bakground.gridToggle();
};
Context.prototype.create = function(command) {
    this.command = command;
    this.hideCommandObjectHotPoint();
    if (this.commandObject instanceof Transition) {
        this.commandObject.remove();
    }
    this.commandObject = null;
};
Context.prototype.setCommandObject = function(commandObject) {
    this.hideCommandObjectHotPoint();
    this.commandObject = commandObject;
};
Context.prototype.hideCommandObjectHotPoint = function() {
    if (this.commandObject != null && this.commandObject.hotPoint != null && jQuery.isFunction(this.commandObject.hotPoint.hide)) {
        this.commandObject.hotPoint.hide();
    }
};

// 右键菜单点击事件，具体方法中覆写
Context.prototype.menuClick = function(objName, action, obj) {
	
};

Context.prototype.objClick = function(event, objType, obj) {
    
};

Context.prototype.objMouseover = function(event, objType, obj) {
	
};

Context.prototype.objMouseout = function(event, objType, obj) {
	
};

// 更新活动属性
Context.prototype.updateActivity = function(activity) {
    var textAttr = activity.viewText.attr();
    activity.viewText.remove();
    var name = activity.alias;
//    if (activity.alias) {
//        name = name + "[" + activity.alias + "]";
//    }
    activity.viewText = context.paper.text(textAttr.x, textAttr.y, name);
    activity.viewText.attr({ "font-size": 16, "font-family": "Arial, Helvetica, sans-serif", "font-weight": "bold"});
};

//更新活动属性
Context.prototype.refashActivity = function(activity) {
   var node = this.getActivity(activity);
   node.name = activity.name;
   node.isAsync = activity.isAsync;
   node.isMultiTask = activity.isMultiTask;
   node.url = activity.url;
   node.clazz = activity.clazz;
   node.alias = activity.name;
   node.type = activity.type;
   //context.paper.text(node.x, node.y, node.name);
};

// 绘制已有活动和路由
Context.prototype.drawActivityAndTransition = function(activityList, transitionList) {
    for (var i = 0; i < activityList.length; i++) {
        this.command = activityList[i].clazz;
        activityList[i].x = parseInt(activityList[i].position.split(":")[0]);
        activityList[i].y = parseInt(activityList[i].position.split(":")[1]);
        if (activityList[i].eventList) {
	        $.each(activityList[i].eventList, function() {
	            activityList[i][this.eventType] = this.actionList;
	        });
        }
        this.setCommandObject(CommandCreateNode(this, activityList[i]));
        $.extend(this.commandObject, activityList[i]);
    }
    this.commandObject = null;
    this.command = "line";
    for (var j = 0; j < transitionList.length; j++) {
        var startActivity = this.getActivityBySid(transitionList[j].fromSid), transition;
        if (startActivity) {
            HotPoint_CreateTransition(startActivity.hotPoint.middleRight);
            transition = this.commandObject;
            transition.conditionList = transitionList[j].decisionConditionList;
            if (transitionList[j].eventList) {
            	transition["transition"] = transitionList[j].eventList[0] ? transitionList[j].eventList[0].actionList : [];
            }
            $.extend(transition, transitionList[j]);
        }
        var endActivity = this.getActivityBySid(transitionList[j].toSid);
        if (endActivity) {
            HotPoint_CreateTransition(endActivity.hotPoint.middleLeft);
        }
        var pathList = transitionList[j].position.split(";"), path = [];
        if (pathList.length > 1) {
	        for (var k = 0; k < pathList.length; k++) {
	            var pointList = pathList[k].split(",");
	            pointList[1] = parseInt(pointList[1]);
	            pointList[2] = parseInt(pointList[2]);
	            path.push(pointList);
	        }
	        if (transition) {
	            transition.view.attr("path", path);
	        }
        }
    }
    this.command = null;
};

Context.prototype.getActivityBySid = function(sid) {
    for (var i = 0; i < this.processDefinition.activityList.length; i++) {
        if (this.processDefinition.activityList[i].sid == sid) {
            return this.processDefinition.activityList[i];
        }
    }
    return null;
};

Context.prototype.removeTransitionHotPoint = function() {
    for (var i = 0; i < context.processDefinition.transitionList.length; i++) {
        context.processDefinition.transitionList[i].removeHotPoint();
    }
};
